package com.busdepot.webbapp.domain;

import java.util.List;
import java.util.Random;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.busdepot.webapp.domain.BusDepotPart;
import com.busdepot.webapp.domain.CatalogService;
import com.busdepot.webapp.domain.Category;
import com.busdepot.webapp.domain.Group;
import com.busdepot.webapp.domain.Model;
import com.busdepot.webapp.domain.ProductGroup;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( { "/spring-service.xml" })

public class CatalogServiceTestIntegration {
	
	private Logger logger = Logger.getLogger(ShippingServiceTestIntegration.class);
	
	@Autowired
	private ApplicationContext applicationContext;
	
	@Autowired
	private CatalogService catalogService;
	
	// category id's = 1-48
	
	// group id's = 1-165
	
	@Test
	public void getModelsByName(){
		String name = "rabbit";
		List<Model> modelList = catalogService.getModelsByName(name);
		
		if(logger.isDebugEnabled()){
			logger.debug("");
			logger.debug("Begin getModelsByName() test");
		}
		
		for(Model model : modelList){
			if(logger.isDebugEnabled()){
				logger.debug("Model name: " + model.getName());
				logger.debug("Start year: " + model.getStartYear());
				logger.debug("End Year: " + model.getEndYear());
				logger.debug("Model Id: " + model.getId());	
			}
		}
	}
	
	@Test
	public void getModelId(){
		String model = "Type 1";
		int startYear = 1947;
		int endYear = 1966;
		int id = catalogService.getModelId(model, startYear, endYear);
		if(logger.isDebugEnabled()){
			logger.debug("");
			logger.debug("Begin getModelId() Test");
			logger.debug("Id is: " + id);
		}
	}
	
	@Test
	public void getCategoryByModeId(){
		
		Random random = new Random();
		// model id's = 1-59 & 101-140
		int pickId = random.nextInt(59);
		List<Category> categoryList = catalogService.getCategoryByModelId(pickId);
		if(logger.isDebugEnabled()){
			logger.debug("");
			logger.debug("Begin getCategoryByModelId() Test");
			logger.debug("Model ID # " + pickId + " has the follow categories: ");
		}
		for(Category category : categoryList){
			if(logger.isDebugEnabled()){
				logger.debug(category.getName());
			}
		}
	}
	
	@Test
	public void getPartsByCategoryId(){
		Random random = new Random();
		// model id's = 1-59 & 101-140
		int pickId = random.nextInt(59);
		// pick a random model
		List<Category> categoryList = catalogService.getCategoryByModelId(pickId);
		int categoryListSize = categoryList.size();
		// pick a random category
		int pickList = random.nextInt(categoryListSize);
		Category category = categoryList.get(pickList);
		int id = category.getId(); 
		List <BusDepotPart> partList = catalogService.getPartsByCategoryId(id);
		
		if(logger.isDebugEnabled()){
			logger.debug("");
			logger.debug("Begin getPartsByCategoryId() test");
			logger.debug("Total parts in this list: " + partList.size());
		}
		
		for(BusDepotPart part : partList){
			if(logger.isDebugEnabled()){
				logger.debug("Part Name: " + part.getName());
				logger.debug("Part Number: " + part.getPartNumber());
			}
		}
		
	}
	
	@Test
	public void getPartsByGroupId(){
		Random random = new Random();
		// model id's = 1-59 & 101-140
		int pickModelId = random.nextInt(59);
		// pick a random model
		List<Category> categoryList = catalogService.getCategoryByModelId(pickModelId);
		int categoryListSize = categoryList.size();
		// pick a random category
		int pickCategoryList = random.nextInt(categoryListSize);
		Category category = categoryList.get(pickCategoryList);
		int pickCategoryId = category.getId();
		// get the list of groups for that category and model
		List <Group> groupList = catalogService.getGroup(pickCategoryId, pickModelId);
		int groupListSize = groupList.size();
		int pickGroupList = random.nextInt(groupListSize);
		// get a group at random
		Group group = groupList.get(pickGroupList);
		int groupId = group.getId();
		List<BusDepotPart> partList = catalogService.getPartsByGroupId(groupId);
		
		if(logger.isDebugEnabled()){
			logger.debug("");
			logger.debug("Begin getPartsByGroupId() test");
			logger.debug("Total parts in this part list: " + partList.size());
		}
		
		for(BusDepotPart part : partList){
			if(logger.isDebugEnabled()){
				logger.debug("Part Name: " + part.getName());
				logger.debug("Part Number: " + part.getPartNumber());
			}
		}
		
	}
	
	@Test
	public void getPartsByProductGroupId(){
		Random random = new Random();
		// model id's = 1-59 & 101-140
		int pickModelId = random.nextInt(59);
		// pick a random model
		List<Category> categoryList = catalogService.getCategoryByModelId(pickModelId);
		int categoryListSize = categoryList.size();
		// pick a random category
		int pickCategoryList = random.nextInt(categoryListSize);
		Category category = categoryList.get(pickCategoryList);
		int pickCategoryId = category.getId();
		// get the list of groups for that category and model
		List <Group> groupList = catalogService.getGroup(pickCategoryId, pickModelId);
		int groupListSize = groupList.size();
		int pickGroupList = random.nextInt(groupListSize);
		// get a group at random
		Group group = groupList.get(pickGroupList);
		int pickGroupId = group.getId();
		List<ProductGroup> productGroupList = catalogService.getProductGroup(pickGroupId, pickCategoryId, pickModelId);
		int productGroupListSize = productGroupList.size();
		int pickProductGroupList = random.nextInt(productGroupListSize);
		// get a product group at random
		ProductGroup productGroup = productGroupList.get(pickProductGroupList);
		int productGroupId = productGroup.getId();
		List<BusDepotPart> partList = catalogService.getPartsByProductGroupId(productGroupId);
		
		if(logger.isDebugEnabled()){
			logger.debug("");
			logger.debug("Begin getPartsByProductGroupId() test");
			logger.debug("Total parts in this part list: " + partList.size());
		}
		
		for(BusDepotPart part : partList){
			if(logger.isDebugEnabled()){
				logger.debug("Part Name: " + part.getName());
				logger.debug("Part Number: " + part.getPartNumber());
			}
		}
	}
	
	
}
